package top.zackyoung.bbs.bean;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import top.zackyoung.bbs.dao.TagDao;
import top.zackyoung.bbs.entity.Tag;

import java.util.*;

/**
 * @author ZackYoung
 * @version 1.0
 * @date 2021/10/5
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Init implements CommandLineRunner {
    final TagDao tagDao;
    List<String> tags = new ArrayList<String>() {{
        add("综合");
        add("测试");
    }};

    @Override
    public void run(String... args) throws Exception {
        // 初始化标签
        tags.stream().filter(x-> !Optional.ofNullable(tagDao.findByName(x)).isPresent())
                .forEach(x->tagDao.save(Tag.of(x)));
        System.out.println("初始化标签完成");
    }
}
